package com.lanzhou.yuanfen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanzhou.yuanfen.diary.entity.Mood;
import com.lanzhou.yuanfen.diary.service.IMoodService;
import com.lanzhou.yuanfen.response.ServerResponsePage;
import com.lanzhou.yuanfen.response.ServerResponseResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/mood/")
public class MoodController {

    @Resource
    private IMoodService iMoodService;


    /**
     * 获取所有的心情
     *
     * @return
     */
    @PostMapping("getAllMood")
    public ServerResponseResult getAllMood() {
        List<Mood> list = iMoodService.redisCache();
        return ServerResponseResult.success(list);
    }

    /**
     * 添加/修改心情
     *
     * @param mood
     * @return
     */
    @PostMapping("addOrUpdateMood")
    public ServerResponseResult addOrUpdateMood(Mood mood) {
        mood.setUpdateTime(LocalDateTime.now());
        boolean save = iMoodService.saveOrUpdate(mood);
        return save ? ServerResponseResult.success(mood) : ServerResponseResult.fail();
    }


    /**
     * 删除文章
     *
     * @param moodKey
     * @return
     */
    @PostMapping("delMood")
    public ServerResponseResult delMood(@RequestParam("moodKey") String moodKey) {
        boolean save = iMoodService.removeById(moodKey);
        return save ? ServerResponseResult.success() : ServerResponseResult.fail();
    }


    /**
     * 添加文章
     *
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @PostMapping("pageGetMood")
    public ServerResponsePage pageGetMood(@RequestParam("pageSize") Integer pageSize,
                                          @RequestParam("pageIndex") Integer pageIndex) {
        ServerResponsePage<Mood> responsePage = new ServerResponsePage<>();
        IPage<Mood> iPage = new Page<>(pageIndex, pageSize);
        IPage<Mood> page = iMoodService.page(iPage, new QueryWrapper<Mood>()
                .orderByDesc("mood_key"));
        responsePage.setIPage(page);
        return responsePage;
    }


}
